package com.lams.service.archive.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.lams.jdbc.BaseDAO;
import com.lams.jdbc.IBaseDAO;
import com.lams.jdbc.QueryUtils;
import com.lams.service.BaseFacade;
import com.lams.service.archive.IStudentFacade;
import com.lams.util.DateHelper;
import com.lams.util.IdFactory;
import com.lams.util.ResultEntity;

public class StudentFacade extends BaseFacade implements IStudentFacade{
	/**
	 * 查询学生SQL编码
	 */
	private String querySql;
	/**
	 * 添加学生SQL编码
	 */
	private String addSql;
	/**
	 * 更新学生SQL
	 */
	private String updateSql;
	/**
	 * 插入学生班级关系
	 */
	private String insertCSSql;
	/**
	 * 更新学生班级关系
	 */
	private String updateCSSql;
//	private IBaseDAO dao;
//	
//	public IBaseDAO getDao() {
//		return dao;
//	}
//
//	public void setDao(IBaseDAO dao) {
//		this.dao = dao;
//	}

	@Override
	public ResultEntity queryStudent(Map params, int start, int limit) {
		String sql = QueryUtils.getSqlBySqlCode(querySql, params);
		int totalCount = QueryUtils.getTotalCount(sql);
		List data = QueryUtils.executeQuery(start, limit, sql);
		ResultEntity result = new ResultEntity();
		result.setData(data);
		result.setTotalCount(totalCount);
		result.setSuccess(true);
		return result;
	}

	@Override
	public void uploadPhoto(File file, String filePath, String fileName) {
		try {
			FileOutputStream fos = new FileOutputStream(filePath+fileName);
			FileInputStream is = new FileInputStream(file);
			byte[] buffer = new byte[1024];
			int len;
			// 将上传文件写入服务器文件中
			while ((len = is.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			is.close();
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public ResultEntity addOrUpdateStudent(Map params, String type) {
		String ssql,cssql;
		ResultEntity result = new ResultEntity();
		if("add".equals(type) || "update".equals(type)){
			if("add".equals(type)){
				params.put("STUDENT_ID", IdFactory.getNextNav("C_STUDENT", "STUDENT_ID"));
				params.put("START_DATE", DateHelper.formatYMD(new Date()));
			}
			ssql = QueryUtils.getSqlBySqlCode("add".equals(type)?this.getAddSql():this.getUpdateSql(), params);
			cssql = QueryUtils.getSqlBySqlCode("add".equals(type)?this.getInsertCSSql():this.getUpdateCSSql(), params);
			String[] sqls = {ssql,cssql};
			int [] flag;
			try {
				flag = super.getDao().batchUpdate(sqls);
				result.setSuccess(true);
				result.setMessage("执行操作成功");
			} catch (SQLException e) {
				e.printStackTrace();
				result.setSuccess(true);
				result.setMessage("执行操作失败");
			}
		} else{
			result.setSuccess(false);
			result.setMessage("更新类型错误");
		}
		return result;
	}
	
	public String getQuerySql() {
		return querySql;
	}

	public void setQuerySql(String querySql) {
		this.querySql = querySql;
	}

	public String getAddSql() {
		return addSql;
	}

	public void setAddSql(String addSql) {
		this.addSql = addSql;
	}

	public String getUpdateSql() {
		return updateSql;
	}

	public void setUpdateSql(String updateSql) {
		this.updateSql = updateSql;
	}

	public String getInsertCSSql() {
		return insertCSSql;
	}

	public void setInsertCSSql(String insertCSSql) {
		this.insertCSSql = insertCSSql;
	}

	public String getUpdateCSSql() {
		return updateCSSql;
	}

	public void setUpdateCSSql(String updateCSSql) {
		this.updateCSSql = updateCSSql;
	}
	
}
